<!DOCTYPE html>
<html lang="en">
	<head>
		<title>three.js webgl - sea3d / keyframe</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<style>
			body {
				font-family: Monospace;
				background-color: #000;
				margin: 0px;
				overflow: hidden;
			}

			#info {
				color: #fff;
				position: absolute;
				top: 10px;
				width: 100%;
				text-align: center;
				z-index: 100;
				display:block;

			}

			a { color: white }
		</style>
	</head>
	<body>
		<div id="info">
			<a href="http://threejs.org" target="_blank">Three.JS</a> - Exported by <a href="https://github.com/sunag/sea3d" style="color:#FFFFFF" target="_blank">SEA3D Exporter</a>
			<br/>
			<br/><div id="description">Click to play</div>
		</div>

		<script src="../build/three.js"></script>

		<script src="js/controls/OrbitControls.js"></script>

		<script src="js/postprocessing/EffectComposer.js"></script>
		<script src="js/postprocessing/RenderPass.js"></script>
		<script src="js/postprocessing/ShaderPass.js"></script>
		<script src="js/postprocessing/MaskPass.js"></script>
		<script src="js/shaders/CopyShader.js"></script>
		<script src="js/shaders/ColorCorrectionShader.js"></script>
		<script src="js/shaders/VignetteShader.js"></script>

		<script src="js/loaders/sea3d/SEA3D.js"></script>
		<script src="js/loaders/sea3d/SEA3DLZMA.js"></script>
		<script src="js/loaders/sea3d/SEA3DLoader.js"></script>

		<script src="js/Detector.js"></script>
		<script src="js/libs/stats.min.js"></script>

		<script>

			if ( ! Detector.webgl ) Detector.addGetWebGLMessage();

			console.log("Visit https://github.com/sunag/sea3d to all codes and builds under development.");

			var container, stats;

			var camera, scene, renderer, composer, controls, demoAt = -1;

			var loader;

			// Initialize Three.JS

			init();

			//
			// SEA3D Loader
			//

			loader = new THREE.SEA3D( {

				autoPlay : false, // Manual play animations
				container : scene // Container to add models

			} );

			loader.onComplete = function( e ) {

				// Get the first camera from 3ds Max
				// use loader.get... to get others objects

				var cam = loader.cameras[0];
				camera.position.copy( cam.position );
				camera.rotation.copy( cam.rotation );

				controls = new THREE.OrbitControls( camera );

				// events

				window.addEventListener('click', onMouseClick, false);

				animate();

			};

			loader.load( './models/sea3d/keyframe.tjs.sea' );

			//
			//	Animation Functions
			//

			function playAll(id, crossfade, offset) {

				// play all animations

				for(var i = 0; i < loader.meshes.length; i++) {

					if (loader.meshes[i].animator)
						loader.meshes[i].animator.play(id, crossfade, offset);

				}

			}

			function setTimeScale( timeScale ) {

				for(var i = 0; i < loader.meshes.length; i++) {

					if (loader.meshes[i].animator)
						loader.meshes[i].animator.setTimeScale( timeScale );

				}

			}

			function stopAll() {

				for(var i = 0; i < loader.meshes.length; i++) {

					if (loader.meshes[i].animator)
						loader.meshes[i].animator.stop();

				}

			}

			//

			function init() {

				scene = new THREE.Scene();

				container = document.createElement( 'div' );
				document.body.appendChild( container );

				camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
				camera.position.set( 1000, - 300, 1000 );

				renderer = new THREE.WebGLRenderer();
				renderer.setPixelRatio( window.devicePixelRatio );
				renderer.setSize( window.innerWidth, window.innerHeight );
				renderer.setClearColor( 0x333333, 1 );
				container.appendChild( renderer.domElement );

				stats = new Stats();
				container.appendChild( stats.dom );

				// post-processing

				composer = new THREE.EffectComposer( renderer );

				var renderPass = new THREE.RenderPass( scene, camera );
				var copyPass = new THREE.ShaderPass( THREE.CopyShader );
				composer.addPass( renderPass );

				var vh = 1.4, vl = 1.2;

				var colorCorrectionPass = new THREE.ShaderPass( THREE.ColorCorrectionShader );
				colorCorrectionPass.uniforms[ "powRGB" ].value = new THREE.Vector3( vh, vh, vh );
				colorCorrectionPass.uniforms[ "mulRGB" ].value = new THREE.Vector3( vl, vl, vl );
				composer.addPass( colorCorrectionPass );

				var vignettePass = new THREE.ShaderPass( THREE.VignetteShader );
				vignettePass.uniforms[ "darkness" ].value = 1.0;
				composer.addPass( vignettePass );

				composer.addPass( copyPass );
				copyPass.renderToScreen = true;

				// events

				window.addEventListener( 'resize', onWindowResize, false );

			}


			function onWindowResize() {

				camera.aspect = window.innerWidth / window.innerHeight;
				camera.updateProjectionMatrix();

				composer.setSize( window.innerWidth, window.innerHeight );
				renderer.setSize( window.innerWidth, window.innerHeight );

			}

			//

			function description(msg) {

				document.getElementById('description').innerHTML = (demoAt + 1) + " - " + msg;

			}

			function onMouseClick( e ) {

				// 0 at 3 demos

				switch( demoAt = ++demoAt % 4 ) {

					// play all animation to sequence "crash#1"

					case 0:

						playAll("crash#1", .5);
						setTimeScale(1);

						description("crash#1 - crossfade 0.5 seconds");

						break;

					// play all animation to sequence "crash#2"

					case 1:

						playAll("crash#2", .5);

						description("crash#2 - crossfade 0.5 seconds");

						break;

					// play animation in single object

					case 2:

						stopAll();

						var fracture99 = loader.getMesh("Object099");

						//fracture99.animator.setTimeScale( 1 );
						//fracture99.animator.playing
						//fracture99.animator.currentAnimation
						//fracture99.animator.previousAnimation

						fracture99.animator.play("crash#2", .5, 0);
						//fracture99.animator.stop();
						//fracture99.animator.pause();
						//fracture99.animator.resume();

						description("crash#2 in single object - crossfade 0.5 seconds");

						break;

					// set time scale

					case 3:

						playAll("crash#2", .5);
						setTimeScale(.1);

						description("time scale / 10 - crossfade 0.5 seconds");

						break;
				}
			}

			//

			var clock = new THREE.Clock();

			function animate() {

				var delta = clock.getDelta();

				requestAnimationFrame( animate );

				// Update SEA3D Animations
				THREE.SEA3D.AnimationHandler.update( delta );

				render( delta );

				stats.update();

			}

			function render( dlt ) {

				//renderer.render( scene, camera );
				composer.render( dlt );

			}

		</script>
	</body>
</html>
